using UnityEngine;

public class AiPlayerLookInitialiser : MonoBehaviour
{
	public RuntimeAnimatorController animatorController;

	public GameObject playerModelPrefab;

	public bool skipShaderInitialization;

	public CustomisationConfig.ShadingType shadingType = CustomisationConfig.ShadingType.Normal;

	public int defaultLookIndex;

	private TableTennisAIPlayer _aiPlayerController;

	private PersonaModel _model;

	private Persona myPersona;

	private TableTennisAIPlayer aiPlayerController
	{
		get
		{
			if (_aiPlayerController == null)
			{
				_aiPlayerController = GetComponent<TableTennisAIPlayer>();
			}
			return _aiPlayerController;
		}
	}

	private PersonaModel model
	{
		get
		{
			if (_model == null)
			{
				_model = GetComponent<PersonaModel>();
			}
			return _model;
		}
	}

	private void CreatePlayerModel()
	{
		GameObject gameObject = Object.Instantiate(playerModelPrefab);
		gameObject.transform.parent = base.transform;
		gameObject.transform.localPosition = Vector3.zero;
		gameObject.transform.localRotation = Quaternion.identity;
		gameObject.GetComponent<Animator>().runtimeAnimatorController = animatorController;
		aiPlayerController.serveHand = gameObject.transform.Search("HAND LEFT");
	}

	private void OnEnable()
	{
		CreatePlayerModel();
		if (!skipShaderInitialization)
		{
			ApplyPlayerModel(shadingType);
		}
	}

	public void ApplyPlayerModel(CustomisationConfig.ShadingType shadingType)
	{
		AILooksDB.AIPlayerLook lookForAIPlayer = GetLookForAIPlayer();
		if (lookForAIPlayer == null)
		{
			return;
		}
		myPersona = new Persona(lookForAIPlayer.gender);
		foreach (CustomisationConfig.Look lookComponent in lookForAIPlayer.lookComponents)
		{
			myPersona.ChangeLook(lookComponent);
		}
		MatchParameters.MatchParams initParameters = MatchParameters.InitParameters;
		PlayerDB.TournamentPlayer opponentDefinition = initParameters.opponentDefinition;
		StoreItemsConfig.StoreItem itemForTypeAndId = GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType.Shoe, opponentDefinition, (opponentDefinition == null) ? string.Empty : opponentDefinition.shoeId);
		CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(CustomisationConfig.LookSetType.Shoes);
		CustomisationConfig.Look lookByName = lookSetForType.GetLookByName(itemForTypeAndId.customisationLookName, lookForAIPlayer.gender);
		myPersona.ChangeLook(lookByName);
		StoreItemsConfig.StoreItem itemForTypeAndId2 = GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType.Racket, opponentDefinition, (opponentDefinition == null) ? string.Empty : opponentDefinition.racketId);
		CustomisationConfig.LookSet lookSetForType2 = CustomisationConfig.instance.GetLookSetForType(CustomisationConfig.LookSetType.Racket);
		CustomisationConfig.Look lookByName2 = lookSetForType2.GetLookByName(itemForTypeAndId2.customisationLookName, Persona.Gender.Male);
		myPersona.ChangeLook(lookByName2);
		CustomisationConfig.instance.ApplyLooksForPersona(model, myPersona, CustomisationConfig.MatchShaderUseType(shadingType, CustomisationConfig.PlayerType.AI));
	}

	private StoreItemsConfig.StoreItem GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType type, PlayerDB.TournamentPlayer aiDefinition, string id)
	{
		StoreItemsConfig.StoreItem storeItem = null;
		if (aiDefinition != null)
		{
			storeItem = StoreItemsConfig.instance.FindItemWithId(id);
		}
		if (storeItem == null)
		{
			storeItem = StoreItemsConfig.instance.GetItemsForType(type, true)[0];
		}
		return storeItem;
	}

	private AILooksDB.AIPlayerLook GetLookForAIPlayer()
	{
		AILooksDB.AIPlayerLook aIPlayerLook = null;
		if (MatchParameters.InitParameters.opponentDefinition != null)
		{
			PlayerDB.TournamentPlayer opponentDefinition = MatchParameters.InitParameters.opponentDefinition;
			aIPlayerLook = opponentDefinition.lookName.look;
		}
		else if (MatchParameters.InitParameters.aiDefinition != null)
		{
			aIPlayerLook = AILooksDB.instance.GetAiLook(MatchParameters.InitParameters.aiDefinition.name);
		}
		if (aIPlayerLook == null && AILooksDB.instance.looks.Count > 0)
		{
			aIPlayerLook = AILooksDB.instance.looks[defaultLookIndex];
		}
		return aIPlayerLook;
	}
}
